package cn.ibobo.lease.app.service.impl;

import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.app.dao.SmsCodeMapper;
import cn.ibobo.lease.common.dto.SmsCodeDTO;
import cn.ibobo.lease.app.service.SmsCodeService;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.Constant;
import cn.ibobo.lease.common.util.DateUtil;
import cn.ibobo.lease.common.util.SMSUtil;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import java.util.List;


/**
 * 短信验证码业务处理接口
 * @Author lililiang
 * @Date 2019-02-21 18:45:20
 */
@Service
public class SmsCodeServiceImpl extends BaseServiceImpl<SmsCodeDTO, String> implements SmsCodeService {

    private static final Log LOGGER = LogFactory.getLog(SmsCodeServiceImpl.class);

    @Autowired
    private SmsCodeMapper mapper;

    @Override
    protected BaseMapper<SmsCodeDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 短信验证码列表
     * @Author lililiang
     * @Date 2019-02-21 18:45:20
     * @Param
     * @Return
     */
    @Override
    public List<SmsCodeDTO> list4Page(SmsCodeDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 短信验证码列表（无分页）
     * @Author lililiang
     * @Date 2019-02-21 18:45:20
     * @Param
     * @Return
     */
    @Override
    public List<SmsCodeDTO> listByCondition(SmsCodeDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询短信验证码详情信息
     * @Author lililiang
     * @Date 2019-02-21 18:45:20
     * @Param
     * @Return
     */
    @Override
    public SmsCodeDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存短信验证码信息
     * @Author lililiang
     * @Date 2019-02-21 18:45:20
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(SmsCodeDTO entity) {
        return super.save(entity);
    }

    /**
     * 删除短信验证码信息
     * @Author lililiang
     * @Date 2019-02-21 18:45:20
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }

    /**
     * @Description 获取短信验证码
     * @Author lililiang
     * @Date 2019/2/21 20:02
     * @Param [mobile]
     * @Return java.lang.Integer
     */
    @Override
    public Integer sendSms(String mobile, String typeCode) {
        String randNum = SMSUtil.getRandNum();
        Integer result = SMSUtil.sendSms(mobile, typeCode, randNum);
        if(result == 0){
            return ResultEnum.FAIL.getCode();
        }
        SmsCodeDTO smsCodeDTO = new SmsCodeDTO();
        smsCodeDTO.setCode(randNum);
        smsCodeDTO.setId(UuidUtil.getUuid());
        smsCodeDTO.setCreatetm(DateUtil.getNow());
        smsCodeDTO.setLosttm(DateUtil.getNextSecond(Constant.SMS_TIME));
        smsCodeDTO.setMobile(mobile);
        return mapper.add(smsCodeDTO);
    }
}
